package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.MenuService;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sys_role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;
    //查询角色权限
    @GetMapping("list")
    private ResultData list(@RequestParam Long page, Long limit, String name){
        log.info("角色管理页码数:{},每页数量:{},角色名称:{}",page,limit,name);
        Page pageInfo = new Page(page, limit);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Role::getName,name);
        queryWrapper.orderByDesc(Role::getCreate_date);
        queryWrapper.orderByDesc(Role::getUpdate_date);
        Long count = Long.valueOf(roleService.count(queryWrapper));
        Page pageData = roleService.page(pageInfo, queryWrapper);
        List data = pageData.getRecords();
        PageResult init = PageResult.init(count, page, limit, data);
        return ResultData.ok("",init);
    }
    //根据id查询数据
    @GetMapping("rowInfo")
    private ResultData rowInfo(@RequestParam  String id){
        log.info("角色管理根据id查询数据,id:{}",id);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,Role::getId,id);
        Role role = roleService.getOne(queryWrapper);

        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(role,roleDto);

        LambdaQueryWrapper<RoleMenu> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(id!=null,RoleMenu::getRole_id,id);
        List<RoleMenu> list = roleMenuService.list(queryWrapper1);

        List<Menu> collect = list.stream().map((roleMenu) -> {
            Integer menuId = roleMenu.getMenu_id();
            LambdaQueryWrapper<Menu> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(menuId != null, Menu::getId, menuId);
            Menu one = menuService.getOne(queryWrapper2);
            return one;
        }).collect(Collectors.toList());
        roleDto.setAuthList(collect);
        return ResultData.ok("",roleDto);
    }

    //查询角色列表全部数据.
    @GetMapping("allData")
    public ResultData allData(){
        log.info("查看角色全部数据");
        List<Role> list = roleService.list();
        return ResultData.ok("查看角色数据成功",list);
    }
    //角色修改
    @PostMapping("update")
    public ResultData update(@RequestBody RoleDto roleDto){
        log.info("角色修改:{}",roleDto);
        if (roleDto.getMenuids()==null){
            return ResultData.ok("数据修改失败","");
        }
        roleService.roleWithMenu(roleDto);
        return ResultData.ok("数据修改成功","");
    }
    //角色增加
    @PostMapping("add")
    public ResultData add(@RequestBody RoleDto roleDto){
        log.info("角色增加,{}",roleDto);
        List<Menu> authList = roleDto.getAuthList();
        roleDto.setCreate_date(LocalDateTime.now());
        List<Integer> menuIds = roleDto.getMenuids();
        roleService.save(roleDto);
        if (authList==null){
            return ResultData.ok("数据增加失败","");
        }
        List<RoleMenu>  roleMenus = authList.stream().map((menu) -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenu_id(menu.getId());
            roleMenu.setRole_id(roleDto.getId());
            roleDto.setCreate_date(LocalDateTime.now());
            return roleMenu;
        }).collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenus);
        roleService.saveWithRoleMenu(roleDto);
        return ResultData.ok("数据增加成功","");
    }
    //角色删除
    @GetMapping("del")
    public ResultData del ( String id){
        log.info("删除角色:{}",id);
        LambdaUpdateWrapper<Role> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(id!=null,Role::getId,id);
        queryWrapper.set(Role::getDel_flag,1);
        roleService.update(queryWrapper);
        return ResultData.ok("数据删除成功,{}","");
    }
}
